import 'dart:async';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:new_yuqib_app/yqbConfig/color_theme.dart';
import 'package:new_yuqib_app/yqbConfig/storage_config.dart';
import 'package:new_yuqib_app/yqbConfig/yqb_toast.dart';
import 'package:new_yuqib_app/yqbNetwork/api_metholds.dart';
import 'package:new_yuqib_app/yqbView/IndexView/view.dart';
import 'package:new_yuqib_app/yqbView/WebView/view.dart';

class LoginViewController extends GetxController {
  //是否勾选协议
  final isChecked = false.obs;

  //手机号码内容
  final phoneController = TextEditingController();

  //验证码
  final codeController = TextEditingController();

  //手机号焦点
  final phoneFocusNode = FocusNode();

  //验证码焦点
  final codeFocusNode = FocusNode();

  //发送验证码
  void sendCode(VoidCallback callback) {
    if (phoneController.text.isEmpty) {
      YqbToast.show("请输入手机号码");
      return;
    }
    if (!phoneController.text.isPhoneNumber) {
      YqbToast.show("请输入正确的手机号码");
      return;
    }
    if (!isChecked.value) {
      showAgreement();
      return;
    }
    EasyLoading.show(status: "发送中...");
    ApiMethods.getCaptcha(phoneController.text).then((value) {
      EasyLoading.dismiss();
      callback.call();
    }).catchError((err) {
      EasyLoading.dismiss();
      print(err);
    });
  }

  @override
  void onInit() {
    super.onInit();
    ApiMethods.activation();
  }

  //发送验证码

  void changeChecked() {
    isChecked.value = !isChecked.value;
  }

  //登录
  void login() {
    if (phoneController.text.isEmpty) {
      YqbToast.show("请输入手机号码");
      return;
    }
    if (!phoneController.text.isPhoneNumber) {
      YqbToast.show("请输入正确的手机号码");
      return;
    }
    if (codeController.text.isEmpty) {
      YqbToast.show("请输入验证码");
      return;
    }
    if (codeController.text.length < 6) {
      YqbToast.show("验证码长度不够");
      return;
    }
    if (!isChecked.value) {
      showAgreement();
      return;
    }
    var putData = {"phone": phoneController.text, "captcha": codeController.text};
    EasyLoading.show(status: "登录中...");
    ApiMethods.login(putData).then((value) {
      EasyLoading.dismiss();
      StorageConfig.token = value.token;
      Get.off(const IndexView());
    }).catchError((err) {
      EasyLoading.dismiss();
      print(err);
    });
  }

  // 协议
  showAgreement() {
    Get.defaultDialog(
        title: "用户协议和隐私政策",
        titleStyle: const TextStyle(color: ColorTheme.primaryTextColor, fontSize: 18),
        contentPadding: const EdgeInsets.symmetric(horizontal: 24),
        content: RichText(
          text: TextSpan(
            text: "为保障与您相关的合法权益，请您阅读并同意",
            style: const TextStyle(color: ColorTheme.primaryTextColor),
            children: [
              TextSpan(
                  text: "《用户协议》",
                  style: const TextStyle(color: Colors.blue),
                  recognizer: TapGestureRecognizer()
                    ..onTap = () {
                      Get.to(const WebView(typeId: 0));
                      debugPrint("点击了《用户协议》");
                    }),
              TextSpan(
                  text: "《隐私政策》",
                  style: const TextStyle(color: Colors.blue),
                  recognizer: TapGestureRecognizer()
                    ..onTap = () {
                      Get.to(const WebView(typeId: 1));
                      debugPrint("点击了《隐私政策》");
                    }),
            ],
          ),
        ),
        textConfirm: "同意",
        onConfirm: () {
          Get.back();
          changeChecked();
        },
        textCancel: "不同意",
        confirmTextColor: Colors.white,
        cancelTextColor: ColorTheme.secondaryTextColor,
        onCancel: Get.back);
  }
}
